package chinaums

import (
	"time"
	"encoding/json"
	"thirdpaysdk/utils/mapstruct"
	//"github.com/astaxie/beego"
)

type MiniPayRequest struct {
	Package   string   `json:"package"`
	AppId     string   `json:"appId"`
	NonceStr  string   `json:"nonceStr"`
	TimeStamp string   `json:"timeStamp"`
	SignType  string   `json:"signType"`
	PaySign   string   `json:"paySign"`
}

type MicroAppPayResponse struct {
	ConnectSys        string         `json:"connectSys"` 
	ErrCode           string         `json:"errCode"` 
	MsgSrc            string         `json:"msgSrc"`
	ResponseTimestamp string         `json:"responseTimestamp"` 
	Mid               string         `json:"mid,omitempty"`
	Tid               string         `json:"tid,omitempty"`
	//
	MsgType           string         `json:"msgType,omitempty"`
	MsgId             string         `json:"msgId,omitempty"`
	ErrMsg            string         `json:"errMsg,omitempty"` 
	SrcReserve        string         `json:"srcReserve,omitempty"`
	MerName           string         `json:"merName,omitempty"`
	MerOrderId        string         `json:"merOrderId,omitempty"`
	SeqId             string         `json:"seqId,omitempty"` 
	SettleRefId       string         `json:"settleRefId,omitempty"`
	Status            string         `json:"status,omitempty"`
	TotalAmount       int            `json:"totalAmount,omitempty"`
	TargetOrderId     string         `json:"targetOrderId,omitempty"` 
	TargetSys         string         `json:"targetSys,omitempty"`
	TargetStatus      string         `json:"targetStatus,omitempty"` 
	TargetMid         string         `json:"targetMid,omitempty"` 
	MiniPayRequest    json.RawMessage `json:"miniPayRequest,omitempty"`
	SignType          string         `json:"signType,omitempty"`
	Sign              string         `json:"sign"`
	DelegatedFlag     string         `json:"delegatedFlag,omitempty"`
}

func MicroAppPay(mid,tid,msgSrc,secreCode,openId,orderId,notifyUrl,attach string,amount int)(rsp *MicroAppPayResponse, err error){
	type Request struct {
		MsgSrc           string  `json:"msgSrc"`
		MsgType          string  `json:"msgType"`
		RequestTimestamp string  `json:"requestTimestamp"`
		MerOrderId       string  `json:"merOrderId"`
		Mid              string  `json:"mid"`
		Tid              string  `json:"tid"`
		InstMid          string  `json:"instMid"`
		TotalAmount      int     `json:"totalAmount"`
		SubOpenId        string  `json:"subOpenId"` 
		TradeType        string  `json:"tradeType"`
		Sign             string  `json:"sign"`
		NotifyUrl        string  `json:"notifyUrl,omitempty"`
		AttachedData     string  `json:"attachedData,omitempty"`
	}
	var request Request
	request.MsgSrc = msgSrc
	request.MsgType = "wx.unifiedOrder"
	request.RequestTimestamp = time.Now().Format("2006-01-02 15:04:05")
	request.MerOrderId = orderId
	request.Mid = mid
	request.Tid = tid
	request.InstMid = "MINIDEFAULT"
	request.TotalAmount = amount
	request.SubOpenId = openId
	request.TradeType = "MINI"
	request.NotifyUrl = notifyUrl
	request.AttachedData = attach
	reqParams := mapstruct.Struct2Map(request, "json")
	reqURL := "https://qr.chinaums.com/netpay-route-server/api/"
	sdk := NewSDK(reqURL, secreCode)
	rsp = new(MicroAppPayResponse)
	_, err = sdk.RequestMicroApp(reqParams, rsp)
	if err != nil {
		return
	}
	return
}

